/**
 * 我收到的还盘.<br/>
 *
 * @author fsl-gongl-20151107 
 *  修改 1. 使用commTemplUtil.showConfDialog;
 *       2. 去掉写死的参数.
 */
$(function() {

    var defaultPagingObj = {
        total: 20,
        pageSize: 10,
        page: 1
    };

    PAGE_DATA.pagingObj = $.extend(defaultPagingObj, PAGE_DATA.pagingObj);
    // 主要分类：咖啡豆及咖啡相关,等.
    commTemplUtil.initPage();

    // 渲染左侧菜单;
    commTemplUtil.initCommLeftMemu();
    //当前左边的导航的位置
    commTemplUtil.setLeftMenu(3);
    //初始化获取列表
    initialize(1);
    
    //点击搜索
    search_btn();
        // 时间-jquery-UI.
    $(".sel-order-time-1").datepicker();
    $(".sel-order-time-2").datepicker();
    



});

function search_btn() {
    $(".sel-search-btn").click(function() {
        initialize(1);
    });
};

function initialize(page) {
    var onClickObj = {
        onClick: function() {
            var clickPage = $(this).attr("data-page");
            initialize(clickPage);
        }
    };

    var tmplObj = _.template($('#item-template').html());
    var time1 = $(".sel-order-time-1").val() || "";
    var time2 = $(".sel-order-time-2").val() || "";
    time(time1,time2);
    var state = $("#oper_state  option:selected").val() || "";
    $.ajax({
        url: PAGE_CONFIG.requestUrl.counterOffer,
        type: "get",
        dataType: "json",
        data: {
            time1: time1,
            time2: time2,
            state: state,
            page: page,
            pageSize: PAGE_DATA.pagingObj.pageSize,
        },
        success: function(result) {
            var datas = $.extend([], result.data);
            var tmplObjHtml = tmplObj(datas);
            $("#commTabs_1_body").html(tmplObjHtml);
            template1();
            counterOfferDel();
            var tmpConf = $.extend(result.pagingConfData, onClickObj)
            commTemplUtil.initPaging("#comm-paging", tmpConf);
        },
        error: function(xhr, status, error) {
            var tmplObjHtml = tmplObj([]);
            $("#commTabs_1_body").html(tmplObjHtml);
        }
    });
};
//弹出框的事件方法
function template1() {
    // var tmplObj1 = _.template($('#item-template1').html());
    var tmplObj = _.template($('#item-template').html());

    // "处理"--弹出框
    $(".handle").click(function() {

        var eventContext = this;
        var thisId = $(this).attr("code");
        function handleFunc() {

            var time1 = $(".sel-order-time-1").val() || "";
            var time2 = $(".sel-order-time-2").val() || "";
            time(time1,time2);
            var state = $("#oper_state  option:selected").val() || "";
            var page = $("#comm-paging").attr("current-page");
            $.ajax({
                url: PAGE_CONFIG.requestUrl.counterOfferHandle,
                type: "put",
                dataType: "json",
                data: {
                    page:page,
                    time1: time1,
                    time2:time2,
                    state: state,
                    pageSize: PAGE_DATA.pagingObj.pageSize,
                    id: thisId || '',
                    _token: COMM_DATA.csrfToken,
                },
                success: function(result) {
                    datas = $.extend([], result.data);
                    var tmplObjHtml = tmplObj(datas);
                    $("#commTabs_1_body").html(tmplObjHtml);
                    
                },
                error: function(xhr, status, error) {
                    var tmplObjHtml = tmplObj([]);
                    $("#commTabs_1_body").html(tmplObjHtml);
                }
            });

        };

        var confData = {
            title: "注意",
            message: "是否确认处理该条信息?",
            cancel_txt: "取消",
            ok_txt: "确定",
            ok_func: handleFunc,
            ok_context: eventContext,
            ok_param: ''
        };
        commTemplUtil.showConfDialog(confData);

    });
};
//删除的事件
function counterOfferDel() {
    var tmplObj = _.template($('#item-template').html());

    $(".sel-edit-del").click(function() {

        var eventContext = this;

        function dialogOkFunc() {

            var dataId = $(this).parents("ul").attr("data-id");
            var currentPage = $("#comm-paging").attr("current-page");
            var time1 = $(".sel-order-time-1").val() || "";
            var time2 = $(".sel-order-time-2").val() || "";
            time(time1,time2);
            var state = $("#oper_state  option:selected").val() || "";
            $.ajax({
                url: PAGE_CONFIG.requestUrl.counterOfferDel,
                type: "delete",
                data: {
                    id: dataId || "",
                    time1: time1,
                    time2:time2,
                    state: state,
                    page: currentPage,
                    pageSize: PAGE_DATA.pagingObj.pageSize,
                    _token: COMM_DATA.csrfToken
                },
                success: function(result) {
                    var datas = $.extend([], result.data);
                    var tmplObjHtml = tmplObj(datas);
                    $("#commTabs_1_body").html(tmplObjHtml);
                },
                error: function() {
                    commTemplUtil.showErrorDialog({
                        title: '注意！',
                        message: '删除失败！',
                        ok_txt: "确定"
                    });
                }
            });
        };

        var confData = {
            title: "注意",
            message: "确认删除此条数据?",
            cancel_txt: "取消",
            ok_txt: "确定",
            ok_func: dialogOkFunc,
            ok_context: eventContext,
            ok_param: ''
        };

        commTemplUtil.showConfDialog(confData);

        return false;
    });
};
function time(time1,time2) {
    
    if (!(time1 == "" || time2 == "")) {
        // 只有输入两个时间 才需要判断 时间合法性.
        var chkFlag = commTemplUtil.checkDate(time1, time2);

        if (!chkFlag) {
            // 不合法--date1 > date2
            commTemplUtil.showErrorDialog({
                title: "注意",
                message: "请检查时间先后顺序!",
                ok_txt: "确定"
            });
            return false;
        }
    }
}